package handler

import (
	"log"
	"strconv"
	"time"
	db "ulcdkey/db"
	"ulcdkey/tool"

	"github.com/labstack/echo/v4"
)

// 获取所有游戏列表
func GetGameList(c echo.Context) error {
	var r ResponseResult
	t := time.Now()
	uid := tool.GetUid(c)

	games := new(db.Game)

	as, err := games.GetGameList()
	if err != nil {
		log.Println("GetGameList1:", err)
		r.Code = -1
		r.Msg = "获取游戏列表失败,err=" + err.Error()
		return c.JSON(200, r)
	}
	r.Data = as
	log.Println(c.Request().URL, "uid =", uid, "【耗时:", time.Now().Sub(t).Seconds(), "s】")
	return c.JSON(200, r)
}

// 获取用户权限游戏列表
func GetAuthGameList(c echo.Context) error {
	var r ResponseResult
	t := time.Now()
	uid := tool.GetUid(c)

	games := new(db.Game)

	as, err := games.GetUserAuthGameList(uid)
	if err != nil {
		log.Println("GetGameList1:", err)
		r.Code = -1
		r.Msg = "获取游戏列表失败,err=" + err.Error()
		return c.JSON(200, r)
	}
	r.Data = as
	log.Println(c.Request().URL, "uid =", uid, "【耗时:", time.Now().Sub(t).Seconds(), "s】")
	return c.JSON(200, r)
}

//// 根据平台获取游戏列表
//func GetGameListByPlatform(c echo.Context) error {
//	var r ResponseResult
//	t := time.Now()
//	uid := tool.GetUid(c)
//
//	games := new(db.Game)
//	//games.Platform = tool.GetHeaderPlatform(c)
//	as, err := games.GetGameListByPlatform()
//	if err != nil {
//		log.Println("GetGameListByPlatform err:", err)
//		r.Code = -1
//		r.Msg = "获取游戏列表失败,err=" + err.Error()
//		return c.JSON(200, r)
//	}
//	r.Data = as
//	log.Println(c.Request().URL, "uid =", uid, "【耗时:", time.Now().Sub(t).Seconds(), "s】")
//	return c.JSON(200, r)
//}

// 新增游戏
func AddGame(c echo.Context) (err error) {
	var r ResponseResult

	t := time.Now()
	uid := tool.GetUid(c)
	games := new(db.Game)
	userInfo := tool.GetVisitUserInfo(c)

	iconIdStr := c.FormValue("iconId")
	games.GameName = c.FormValue("gameName")
	games.GameCode = c.FormValue("gameCode")
	games.IconId, err = strconv.Atoi(iconIdStr)
	if err != nil {
		log.Println("iconId参数错误 err:", err)
		r.Code = -1
		r.Msg = "iconId参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}
	games.CreateUserId = uid
	games.UpdateUserId = uid
	//games.Platform = tool.GetHeaderPlatform(c)

	err = games.AddGame(userInfo)
	if err != nil {
		log.Println("AddGame err:", err)
		r.Code = -1
		r.Msg = "新增游戏失败,err=" + err.Error()
		return c.JSON(200, r)
	}
	log.Println(c.Request().URL, "uid =", uid, "【耗时:", time.Now().Sub(t).Seconds(), "s】")
	return c.JSON(200, r)
}

// 更新游戏
func UpdateGame(c echo.Context) (err error) {
	var r ResponseResult

	t := time.Now()
	uid := tool.GetUid(c)
	games := new(db.Game)
	userInfo := tool.GetVisitUserInfo(c)

	gameIdStr := c.FormValue("id")
	iconIdStr := c.FormValue("iconId")

	games.GameName = c.FormValue("gameName")
	games.Id, err = strconv.Atoi(gameIdStr)
	if err != nil {
		log.Println("id参数错误 err:", err)
		r.Code = -1
		r.Msg = "id参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}
	games.IconId, err = strconv.Atoi(iconIdStr)
	if err != nil {
		log.Println("iconId参数错误 err:", err)
		r.Code = -1
		r.Msg = "iconId参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}
	games.UpdateUserId = uid
	//games.Platform = tool.GetHeaderPlatform(c)

	//校验用户是否有该游戏权限
	nUser := new(db.User)
	nUser.Id = uid
	flag := nUser.CheckUserGameRole(games.Id)
	if !flag {
		log.Println("用户无权限更新游戏，uid=", uid, games.Id)
		r.Code = -1
		r.Msg = "您无权限操作该游戏"
		return c.JSON(200, r)
	}

	// 更新游戏 不允许更新gameCode字段 因为对应的cdk表已经被创建
	err = games.UpdateGame(userInfo)
	if err != nil {
		log.Println("UpdateGame err:", err)
		r.Code = -1
		r.Msg = "更新游戏失败,err=" + err.Error()
		return c.JSON(200, r)
	}
	log.Println(c.Request().URL, "uid =", uid, "【耗时:", time.Now().Sub(t).Seconds(), "s】")
	return c.JSON(200, r)
}

// 删除游戏
func DelGame(c echo.Context) (err error) {
	var r ResponseResult
	t := time.Now()
	uid := tool.GetUid(c)
	games := new(db.Game)
	userInfo := tool.GetVisitUserInfo(c)

	gameIdStr := c.FormValue("id")

	games.Id, err = strconv.Atoi(gameIdStr)
	if err != nil {
		log.Println("id参数错误 err:", err)
		r.Code = -1
		r.Msg = "id参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}

	//校验用户是否有该游戏权限
	nUser := new(db.User)
	nUser.Id = uid
	flag := nUser.CheckUserGameRole(games.Id)
	if !flag {
		log.Println("用户无权限删除游戏，uid=", uid, games.Id)
		r.Code = -1
		r.Msg = "您无权限操作该游戏"
		return c.JSON(200, r)
	}

	err = games.DelGame(userInfo)
	if err != nil {
		log.Println("DelGame err:", err)
		r.Code = -1
		r.Msg = "删除游戏失败,err=" + err.Error()
		return c.JSON(200, r)
	}
	log.Println(c.Request().URL, "uid =", uid, "【耗时:", time.Now().Sub(t).Seconds(), "s】")
	return c.JSON(200, r)
}
